package com.persion.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.persion.common.annotation.Anonymous;
import com.persion.common.core.domain.AjaxResult;
import com.persion.common.utils.SecurityUtils;
import com.persion.common.utils.StringUtils;
import com.persion.domain.FrontCategory;
import com.persion.domain.FrontNews;
import com.persion.dto.Params;
import com.persion.service.FrontCategoryService;
import com.persion.service.FrontNewsService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 资讯(FrontNews)表控制层
 *
 * @author WangFK
 * @since 2024-01-25 19:01:06
 */
@RestController
@Slf4j
@RequestMapping("/manage/news")
public class FrontNewsController {
    /**
     * 服务对象
     */
    @Resource
    private FrontNewsService frontNewsService;
    @Resource
    private FrontCategoryService frontCategoryService;

    /**
     * 根据分类id查询文章
     * @param CategoryId
     * @return
     */
    @Anonymous
    @GetMapping("/getNewByCategoryId")
    public AjaxResult getNewByCategoryId(String CategoryId){
        return AjaxResult.success(frontNewsService.list(new LambdaUpdateWrapper<FrontNews>()
                .eq(ObjectUtil.isNotEmpty(CategoryId),FrontNews::getCategoryId,CategoryId)
                .eq(FrontNews::getOpStatus,"0")));
    }
    /**
     * 上下架分类状态
     * @param frontNews
     * @return
     */
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody FrontNews frontNews){
        LambdaUpdateWrapper<FrontNews> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(FrontNews::getNewsId,frontNews.getNewsId())
                .set(FrontNews::getOpStatus,frontNews.getOpStatus());
        return frontNewsService.update(lqw)?AjaxResult.success():AjaxResult.warn("修改状态失败");
    }
    /**
     * 分页查询
     * @Author WangFK
     * @Date 2024/1/25 19:11
     * @Param [params]
     * @return com.persion.common.core.domain.AjaxResult
     **/

    @GetMapping("/list")
    public AjaxResult list(Params params){
        Page<FrontNews> frontNewsPage = new Page<>(params.pageNum, params.pageSize);
        LambdaQueryWrapper<FrontNews> lqw = new LambdaQueryWrapper<>();
        String search = params.getSearch();
        lqw.like(StringUtils.isNotEmpty(search),FrontNews::getNewsTitle,search).or()
                .like(StringUtils.isNotEmpty(search),FrontNews::getNewsIntro,search).or()
                .like(StringUtils.isNotEmpty(search),FrontNews::getNewsCreate,search).or()
                .like(StringUtils.isNotEmpty(search),FrontNews::getNewsCount,search).or()
                .like(StringUtils.isNotEmpty(search),FrontNews::getNewsTitle,search)
                .eq(StringUtils.isNotEmpty(params.getCategoryId()),FrontNews::getCategoryId,params.getCategoryId())
                .eq(StringUtils.isNotEmpty(params.getOpStatus()),FrontNews::getOpStatus,params.getOpStatus())
                .like(StringUtils.isNotEmpty(params.getCreateTime()),FrontNews::getCreateTime,params.getCreateTime());
        frontNewsPage = frontNewsService.page(frontNewsPage, lqw);
        List<FrontNews> records = frontNewsPage.getRecords();
        if (StringUtils.isEmpty(records)){
            return AjaxResult.warn("暂无信息");
        }
        return AjaxResult.success()
                .put("rows",records).put("total",records.size());
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult selectOne(@PathVariable Serializable id) {
        return AjaxResult.success(frontNewsService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param frontNews 实体对象
     * @return 新增结果
     */
    @PostMapping
    public AjaxResult insert(@RequestBody FrontNews frontNews) {
        if (StringUtils.isEmpty(frontNews.getNewsTitle())||StringUtils.isEmpty(frontNews.getNewsIntro())||StringUtils.isEmpty(frontNews.getNewsRemark())||StringUtils.isEmpty(frontNews.getNewsPhoto())||StringUtils.isEmpty(frontNews.getCategoryId().toString())){
            return AjaxResult.error("请按规范填写");
        }
        if (ObjectUtil.isNotEmpty(frontNews.getNewsId())){
            return update(frontNews);
        }
        frontNews.setNewsCreate(SecurityUtils.getUsername());
        frontNews.setCategoryName(frontCategoryService.getById(frontNews.getCategoryId()).getFrontName());
        return AjaxResult.success(frontNewsService.save(frontNews));
    }

    /**
     * 修改数据
     *
     * @param frontNews 实体对象
     * @return 修改结果
     */
    @PutMapping
    public AjaxResult update(@RequestBody FrontNews frontNews) {
        if (!frontNews.getCategoryId().toString().equalsIgnoreCase(frontNewsService.getById(frontNews.getNewsId()).getCategoryId().toString())){
            frontNews.setCategoryName(frontCategoryService.getById(frontNews.getCategoryId()).getFrontName());
        }
        return AjaxResult.success(frontNewsService.updateById(frontNews));
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable Serializable id) {
        return AjaxResult.success(frontNewsService.removeById(id));
    }
}

